package com.ruoyi.dept.controller;

import java.util.*;
import java.util.concurrent.TimeUnit;

import cn.dev33.satoken.annotation.SaIgnore;
import com.ruoyi.dept.domain.vo.BsDepartmentTreeVo;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckLogin;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.dept.domain.vo.BsDepartmentVo;
import com.ruoyi.dept.domain.bo.BsDepartmentBo;
import com.ruoyi.dept.service.IBsDepartmentService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 科室
 *
 * @author zyb
 * @date 2024-04-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/dept/bsDepartment")
public class BsDepartmentController extends BaseController {

    private final IBsDepartmentService iBsDepartmentService;

    /**
     * 查询科室列表
     */
    @SaCheckLogin
    @GetMapping("/list")
    public TableDataInfo<BsDepartmentVo> list(BsDepartmentBo bo, PageQuery pageQuery) {
        return iBsDepartmentService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出科室列表
     */
    @SaCheckLogin
    @Log(title = "科室", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BsDepartmentBo bo, HttpServletResponse response) {
        List<BsDepartmentVo> list = iBsDepartmentService.queryList(bo);
        ExcelUtil.exportExcel(list, "科室", BsDepartmentVo.class, response);
    }

    /**
     * 获取科室详细信息
     *
     * @param id 主键
     */
    @SaCheckLogin
    @GetMapping("/{id}")
    public R<BsDepartmentVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iBsDepartmentService.queryById(id));
    }

    /**
     * 新增科室
     */
    @SaCheckLogin
    @Log(title = "科室", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BsDepartmentBo bo) {
        return toAjax(iBsDepartmentService.insertByBo(bo));
    }

    /**
     * 修改科室
     */
    @SaCheckLogin
    @Log(title = "科室", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BsDepartmentBo bo) {
        return toAjax(iBsDepartmentService.updateByBo(bo));
    }

    /**
     * 删除科室
     *
     * @param ids 主键串
     */
    @SaCheckLogin
    @Log(title = "科室", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iBsDepartmentService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 查询科室列表
     */
    @SaCheckLogin
    @GetMapping("/tree")
    public R<List<BsDepartmentTreeVo>> tree(BsDepartmentBo bo) {
        List<BsDepartmentTreeVo> tree = iBsDepartmentService.tree(bo);
        return R.ok(tree);
    }
}
